String.prototype.endWith = function(endStr) {
	var d = this.length - endStr.length;
	return(d >= 0 && this.lastIndexOf(endStr) == d);
}

function checkCollapse(event) {
	//console.log(event);
	var target = event.target;
	//console.log(target);
	var tag = target.localName;
	//console.log(tag);
	//console.log(expectedEvent);
	if(tag == "video") {
		var url = target.src;
		if(!url)
			return;
		initXXVideoPlayer(target,url);
	}
}

function initXXVideoPlayer(video, url) {
	if(!url.endWith(".m3u8") && !url.endWith(".flv")) {
		return;
	}
	var protocol = window.location.protocol;
	if(protocol == "https:" && url.indexOf("https:") !== 0) {
		url = url.replace("http://", "https://");
	}
	//console.log(url);
	var hls = new Hls();
	hls.loadSource(url);
	hls.attachMedia(video);
}

function init() {
	if(!(document.documentElement instanceof HTMLElement))
		return;
	document.addEventListener("DOMNodeInserted", checkCollapse, true);
}
// In Chrome 18 the document might not be initialized yet
if(document.documentElement)
	init();
else
	window.setTimeout(init, 0);